package com.tony.demo.ui;

import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.AppUtils;
import com.puhui.lib.utils.MyToastUtil;
import com.tony.demo.R;
import com.tony.demo.mvp.presenter.impl.MainPresenter;
import com.tony.demo.mvp.views.IMainView;
import com.tony.demo.ui.base.BaseActivity;
import com.tony.demo.ui.framework.FrameworkFragment;
import com.tony.demo.ui.home.HomeFragment;
import com.tony.demo.ui.performance.PerformanceOptimizeFragment;
import com.tony.demo.ui.widgets.UiWidgetsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<MainPresenter> implements IMainView {

    FrameLayout realTabContent;
    RadioButton tabIndex0;
    RadioButton tabIndex1;
    RadioButton tabIndex2;
    RadioButton tabIndex3;
    RadioGroup mainRadioGroup;

    private List<Fragment> fragmentList;
    private Fragment currentFragment;

    private long lastClickTime;  //保存上一次点击返回按钮的时间，用来判断是否退出应用

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_main;
    }

    protected void initView() {
        MyToastUtil.getInstance().register(this);

        realTabContent = findViewById(R.id.real_tab_content);
        tabIndex0 = findViewById(R.id.tabIndex0);
        tabIndex1 = findViewById(R.id.tabIndex1);
        tabIndex2 = findViewById(R.id.tabIndex2);
        tabIndex3 = findViewById(R.id.tabIndex3);
        mainRadioGroup = findViewById(R.id.mainRadioGroup);

        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new FrameworkFragment());
        fragmentList.add(new PerformanceOptimizeFragment());
        fragmentList.add(new UiWidgetsFragment());

        mainRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                doOnCheckedChanged(checkedId);
            }
        });

        mainRadioGroup.check(R.id.tabIndex0);
    }

    /**
     * @param checkedId 被选中的tab
     */
    private void doOnCheckedChanged(int checkedId) {
        if (checkedId == R.id.tabIndex0) {
            switchFragment(currentFragment, fragmentList.get(0));
        } else if (checkedId == R.id.tabIndex1) {
            switchFragment(currentFragment, fragmentList.get(1));
        } else if (checkedId == R.id.tabIndex2) {
            switchFragment(currentFragment, fragmentList.get(2));
        } else if (checkedId == R.id.tabIndex3) {
            switchFragment(currentFragment, fragmentList.get(3));
        }
    }

    /**
     * 切换界面
     *
     * @param from 当前界面
     * @param to   切换后的界面
     */
    private void switchFragment(Fragment from, Fragment to) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (from == null) {
            from = fragmentList.get(0);
            currentFragment = from;
            transaction.add(R.id.real_tab_content, from);
        } else {
            // mainRadioGroup.check(R.id.tabIndex0); 该语句内部会调用两次RadioGroup的
            // setCheckedStateForView()方法，导致onCheckedChanged()方法会执行两次。
            // 导致此时的from和to是相同的。
            if (from == to) {
                return;
            }

            if (to.isAdded()) {
                transaction.show(to).hide(from);
            } else {
                transaction.add(R.id.real_tab_content, to).hide(from);
            }
            currentFragment = to;
        }
        transaction.commit();
    }

    @Override
    public void onDataSuccess() {
//        showToast("哈哈哈哈");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long clickTime = System.currentTimeMillis();
            if (clickTime - lastClickTime <= 2000) {
                MyToastUtil.getInstance().unRegister();
                AppUtils.exitApp();
            } else {
                lastClickTime = clickTime;
                MyToastUtil.getInstance().show("再按一次退出");
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
